package com.hsy.linemoney.modules.auth.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.deparse.dpframe.tools.utils.DPLogUtil;
import com.hsy.linemoney.R;
import com.hsy.linemoney.apiservice.AddReferenceId;
import com.hsy.linemoney.apiservice.Callback;
import com.hsy.linemoney.apiservice.LineMoneyContext;
import com.hsy.linemoney.apiservice.RegistrationService;
import com.hsy.linemoney.apiservice.SignUpService;
import com.hsy.linemoney.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.linemoney.modules.main.MainActivity;
import com.hsy.linemoney.utils.CommonUtils;

/**
 * Created by MartinKent on 2016/11/21.
 */

public class AgreementActivity extends CommonBaseActivityWithSimpleActionBar implements View.OnClickListener {

    private WebView webView;
    private TextView agree;
    private TextView disagree;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(R.string.txt_terms_of_use);
        setContentView(R.layout.activity_agreement);

        hideProgressDialog();

        webView = (WebView) findViewById(R.id.web_view);
        agree = (TextView) findViewById(R.id.agree);
        disagree = (TextView) findViewById(R.id.disagree);

        agree.setOnClickListener(this);
        disagree.setOnClickListener(this);

//        webView.setWebChromeClient(new WebChromeClient() {
//
//        });http://qa.linemoney.com/c/portal/terms_of_use
//        webView.loadUrl(LineMoneyContext.getServer() + "c/portal/terms_of_use");
//        webView.loadUrl("http://qa.linemoney.com/c/portal/terms_of_use");

//        webView.setWebViewClient(new WebViewClient() {
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                DPLogUtil.d("AgreementActivity loadUrl=" + url);
////                view.loadUrl(LineMoneyContext.getServer() + "c/portal/terms_of_use");
//                return true;
//            }
//        });
        webView.setWebViewClient(new WebViewClient());

        WebSettings settings = webView.getSettings();
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setJavaScriptEnabled(true);
//        settings.setAppCacheEnabled(true);
        webView.loadUrl(LineMoneyContext.getServer()+"c/portal/terms_of_use");
        showToast("请在此处登录一次");
//        webView.loadUrl("http://qa.linemoney.com/c/portal/terms_of_use");

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.agree) {
            showProgressDialog(AgreementActivity.this);
            final RegistrationService service = new RegistrationService();
            service.setAgreedToTermsOfUse(getActivityStartExtra()[0], new Callback<RegistrationService.SetAgreedToTermsOfUseResult>() {
                @Override
                public void onSuccess(int what, RegistrationService.SetAgreedToTermsOfUseResult result) {
//                    LineMoneyContext.getLoggedUser().setAgreedToTermsOfUse(true);
                    DPLogUtil.d(" onSuccess   AgreementActivity");
                    if (result.getStatus().getCode() == 200) {
                        DPLogUtil.d(" onSuccess 200  AgreementActivity");
                        if (result.getValue().getIsTermsOfUseAgreed()) {
//                            if (LineMoneyContext.isLoggedIn() && !LineMoneyContext.getLoggedUser().getEmailAddressVerified()) {
//                                DPLogUtil.d("getIsUseComplete onSuccess 200 getIsTermsOfUseAgreed VerifyEmailActivity");
//                                startActivity(VerifyEmailActivity.class);
//                                finish();
//                            } else {
                            DPLogUtil.d("getIsUseComplete onSuccess 200 getIsTermsOfUseAgreed MainActivity");
                            startActivityWithTaskCleared(MainActivity.class);
//                            }
                        }
                    } else {
                        DPLogUtil.d(" onSuccess 500  AgreementActivity");
                        showToast(result.getStatus().getMessage());
                    }
                    hideProgressDialog();
//                    if (!LineMoneyContext.getLoggedUser().getEmailAddressVerified()) {
//                    startActivity(VerifyEmailActivity.class);
//                    } else {
//                    }
                }

                @Override
                public void onError(int what, int type, String msg, Exception e) {
                    hideProgressDialog();
                }
            });
        } else {
            startActivityWithTaskCleared(LoginActivity.class);
        }
    }
}